<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8" />
  <title>公众号支付</title>
  <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />

  <!-- <script src="https://cdn.bootcss.com/vConsole/3.3.0/vconsole.min.js"></script>
    <script>
      new VConsole();
    </script> -->
</head>

<body>
  <div id="gzPay">
    <center>
      <h2>微信支付中...</h2>
    </center>
  </div>
  <script type="text/javascript" src="./js/jquery-3.1.1.min.js"></script>
  <script type="text/javascript">
    var baseUrl = "https://bootapi.51bmj.cn";
    var h5Url = "https://m.51bmj.cn";
    // var baseUrl = "https://bootapi.51bmj.net";
    // var h5Url = "https://m.51bmj.net";

    var redirectUrl = h5Url + getParam("redirectUrl");

    getOpenid();
    //若有传type appId，则为自定义公众号支付；默认为我们的公众号支付
    function getOpenid() {
      $.ajax({
        type: "get",
        //旧 wy56 type 0；新 百驿 type 11 暂时还是用旧的，同parkingFee一样
        //用正式链接，免得测试token覆盖
        url: baseUrl + "/api/openIdByCode?code=" +
          getParam("code"),
        dataType: "json",
        success: function (res) {
          if (res.code == 200) {
            getWChatPayParams(res.result);
          } else {
            alert("获取openId错误");
            window.location = redirectUrl;
          }
        },
        error: function (xhr, type) {
          alert("支付失败");
          window.location = redirectUrl;
        }
      });
    }

    function getWChatPayParams(openId) {
      debugger
      let postData = {
        openId: openId,
        payType: getParam("payType"),
        userId: getParam("userId"),
        applicationNumbers: getParam("applicationNumbers"),
        // customerData: {
        contactNumber: getParam("contactNumber"),
        contactPerson: getParam("contactPerson"),
        mailingAddress: getParam("mailingAddress"),
        receipt: getParam("receipt")
        // }
      }

      let url = baseUrl + "/bmj-wechat/weChat/pay/createOrder";
      console.log('postData', postData)
      $.ajax({
        type: "post",
        url,
        dataType: "json",
        data: JSON.stringify(postData),
        contentType: "application/json;charset=utf-8",
        success: function (res) {
          console.log('wxparams', res)
          if (res.appId) {
            if (typeof window.WeixinJSBridge == "undefined") {
              $(document).on("WeixinJSBridgeReady", function () {
                sendPayRequest(res);
              });
            } else {
              sendPayRequest(res);
            }
          } else {
            setTimeout(() => {
              alert('支付失败');
              window.location = redirectUrl;
            }, 200000)
          }
        },
        error: function (xhr, type) {
          // console.log(xhr)
          // alert("支付请求失败");
          // window.location = redirectUrl;
          setTimeout(() => {
            console.log(xhr)
            alert("支付请求失败");
            window.location = redirectUrl;
          }, 200000)
        }
      });
    }

    function sendPayRequest(res) {
      WeixinJSBridge.invoke(
        "getBrandWCPayRequest", {
          "appId": res.appId, //公众号名称，由商户传入
          "timeStamp": res.timeStamp, //时间戳，自1970年以来的秒数
          "nonceStr": res.nonceStr, //随机串
          "package": res.packageValue,
          "signType": "MD5", //微信签名方式：
          "paySign": res.paySign //微信签名
        },
        function (res) {
          //支付成功
          if (res.err_msg == "get_brand_wcpay_request:ok") {
            window.location.href = redirectUrl;
          } else {
            window.history.go(-1);
          }
        }
      );
    }

    function getParam(name) {
      let path = location.href;
      var reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)", "i");
      if (reg.test(path))
        return unescape(RegExp.$2.replace(/\+/g, " "));
      return "";
    }

  </script>
</body>

</html>
